"""
@Author : 合肥一元数智教育科技有限公司
@Date :  2025/3/21 13:49
@Description :  如何实现参数的动态传递
  模拟用户登录过程
"""
import pymysql

# 获取连接
conn = pymysql.connect(
    host='localhost',
    port=3306,
    user='root',
    passwd='root',
    db='python2511',
    charset='utf8'
)
# 获取游标
cur = conn.cursor()

username = input('username:')
password = input('password:')
# 字符串拼接 实现动态参数   存在sql注入问题
# str_sql = 'select * from user where username = "' + username + '" and password = "' + password + '"'

# %s 占位符   sql预编译  解决sql注入问题  推荐
"""
str_sql = 'select * from user where username = %s and password = %s'
print(str_sql)
line = cur.execute(str_sql, (username, password))
"""

str_sql = 'select * from user where username = %(uname)s and password = %(pwd)s'
print(str_sql)
user_info = {
    'uname': username,
    'pwd': password
}
line = cur.execute(str_sql, user_info)
if line:
    print('登录成功')
else:
    print('用户名或密码错误...')

#  释放资源    每次执行sql完毕后 都需要将连接 游标资源释放  频繁创建连接和释放连接的过程  消耗数据库的性能    数据库连接池
cur.close()
conn.close()
